﻿using RobotPratolSolution.ViewModels;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;

namespace RobotPratolSolution.Pages
{
    /// <summary>
    /// CronManage.xaml 的交互逻辑
    /// </summary>
    public partial class CronManage : UserControl
    {
        CronManageViewModel CronManageViewModel { get; set; }
        public CronManage()
        {
            InitializeComponent();
            CronManageViewModel = new CronManageViewModel();
            this.DataContext = CronManageViewModel;
        }

        private void AddTimeItem_Click(object sender, RoutedEventArgs e)
        {
            int Jobindex = JobListBox.SelectedIndex;
            if (Jobindex < 0)
            {
                MessageBox.Show("需要选择任务！");
                return;
            }
            if (string.IsNullOrEmpty(CronManageViewModel.TakedJobTime))
            {
                MessageBox.Show("需要写入时间！");
                return;
            }
            else
            {
                bool istime = IsTime(CronManageViewModel.TakedJobTime);
                if (!istime)
                {
                    MessageBox.Show("需要写入有效时间！");
                    return;
                }
            }
            if (CronManageViewModel.CronDataList.ToList().Find(a => a.Time == CronManageViewModel.TakedJobTime) != null)
            {
                MessageBox.Show("输入时间不能相同！");
                return;
            }
            CronManageViewModel.AddTimeJobItem(Jobindex);
        }


        /// <summary>
        /// 是否为时间型字符串
        /// </summary>
        /// <param name="source">时间字符串(15:00:00)</param>
        /// <returns></returns>
        public static bool IsTime(string StrSource)
        {
            return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d)$");
        }

        private void DeleteTimeItem_Click(object sender, RoutedEventArgs e)
        {
            //int timeitemIndex = JobTimeDataGrid.SelectedIndex;
            var data = JobTimeDataGrid.SelectedItem as JobTimeInfo;
            if (data != null)
                CronManageViewModel.RemoveTimeJobItem(data.Id.ToString());
        }

        private void RefreshTimeItem_Click(object sender, RoutedEventArgs e)
        {
            CronManageViewModel.CronListRefresh();
            CronManageViewModel.JobListInfoRefresh();

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            CronManageViewModel.CronListRefresh();
            CronManageViewModel.JobListInfoRefresh();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // int index = JobTimeDataGrid.SelectedItem;
            //CronManageViewModel.CronDataList[index].IsEnable = !CronManageViewModel.CronDataList[index].IsEnable;

            var data = JobTimeDataGrid.SelectedItem as JobTimeInfo;
            if (data != null)
                CronManageViewModel.UpdateCronUseable(data.Id.ToString());


        }
    }
}
